package com.imut.lagain.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 响应格式化工具类
 */
public class ResponseUtil {
    private static final Logger log = LoggerFactory.getLogger(ResponseUtil.class);

    
    /**
     * 成功响应
     * @param data 数据
     * @return 响应Map
     */
    public static Map<String, Object> success(Object data) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "操作成功");
        response.put("data", data);
        return response;
    }
    
    /**
     * 成功响应（无数据）
     * @return 响应Map
     */
    public static Map<String, Object> success() {
        return success(null);
    }
    
    /**
     * 成功响应（自定义消息）
     * @param message 消息
     * @param data 数据
     * @return 响应Map
     */
    public static Map<String, Object> success(String message, Object data) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", message);
        response.put("data", data);
        return response;
    }
    
    /**
     * 失败响应
     * @param message 错误消息
     * @return 响应Map
     */
    public static Map<String, Object> error(String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", false);
        response.put("message", message);
        response.put("data", null);
        return response;
    }
    
    /**
     * 失败响应（带错误码）
     * @param code 错误码
     * @param message 错误消息
     * @return 响应Map
     */
    public static Map<String, Object> error(int code, String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", false);
        response.put("code", code);
        response.put("message", message);
        response.put("data", null);
        return response;
    }
    
    /**
     * 分页响应
     * @param data 数据列表
     * @param total 总数
     * @param page 当前页
     * @param size 每页大小
     * @return 响应Map
     */
    public static Map<String, Object> page(Object data, long total, int page, int size) {
        Map<String, Object> pageInfo = new HashMap<>();
        pageInfo.put("records", data);
        pageInfo.put("total", total);
        pageInfo.put("current", page);
        pageInfo.put("size", size);
        pageInfo.put("pages", (total + size - 1) / size);
        
        return success(pageInfo);
    }
    
    /**
     * 验证失败响应
     * @param errors 验证错误信息
     * @return 响应Map
     */
    public static Map<String, Object> validationError(Map<String, String> errors) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", false);
        response.put("message", "参数验证失败");
        response.put("errors", errors);
        return response;
    }
    
    /**
     * 未授权响应
     * @return 响应Map
     */
    public static Map<String, Object> unauthorized() {
        return error(401, "未授权访问");
    }
    
    /**
     * 禁止访问响应
     * @return 响应Map
     */
    public static Map<String, Object> forbidden() {
        return error(403, "禁止访问");
    }
    
    /**
     * 资源未找到响应
     * @return 响应Map
     */
    public static Map<String, Object> notFound() {
        return error(404, "资源未找到");
    }
    
    /**
     * 服务器内部错误响应
     * @return 响应Map
     */
    public static Map<String, Object> internalError() {
        return error(500, "服务器内部错误");
    }
}
